package com.smalljj.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smalljj.bean.Message;
import com.smalljj.service.MessageService;
import com.smalljj.util.SmalljjUtil;

@Controller
@RequestMapping("message")
public class MessageController {

	@Autowired
	private MessageService messageService;
	
	/**
	 * 收件箱
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping( value="/myRmessage",method=RequestMethod.POST )
    @ResponseBody
	public List<Map<String,Object>> myrRmessage(HttpServletRequest request) throws Exception {
		String userIdBase64 = request.getParameter("userId");
		String userId = SmalljjUtil.decodeBase64(userIdBase64);
		List<Map<String,Object>> messageList = messageService.getRMessageList(userId);
		return messageList;
	}
	/**
	 * 发件箱
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping( value="/mySmessage",method=RequestMethod.POST )
    @ResponseBody
	public List<Map<String,Object>> myrSmessage(HttpServletRequest request) throws Exception {
		String userIdBase64 = request.getParameter("userId");
		String userId = SmalljjUtil.decodeBase64(userIdBase64);
		List<Map<String,Object>> messageList = messageService.getSMessageList(userId);
		return messageList;
	}
	/**
	 * 删短信
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping( value="/delRMessage",method=RequestMethod.POST )
    @ResponseBody
	public Map<String,Object> delRMessage(HttpServletRequest request) throws Exception {
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		String messageIdBase64 = request.getParameter("messageId");
		String messageId = SmalljjUtil.decodeBase64(messageIdBase64);
		String deltype = request.getParameter("deltype");
		Message message =  new Message();
		message.setId(new BigDecimal(messageId));
		if(deltype.equals("adel")){
			message.setAdel(new BigDecimal(1));
		}else if(deltype.equals("bdel")){
			message.setBdel(new BigDecimal(1));
		}
		boolean result = messageService.update(message);
		if(result){
			jsonMap.put("result", true);
			jsonMap.put("message", "删除成功!");
			return jsonMap;
		}else{
			jsonMap.put("result", false);
			jsonMap.put("message", "删除失败!");
			return jsonMap;
		}
	}
	
}